<template>
  <div class="user-edit">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="ruleForm.nickName"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="userName">
        <el-input v-model="ruleForm.userName"></el-input>
      </el-form-item>
      <el-form-item label="是否注销" prop="status">
        <el-radio-group v-model="ruleForm.status">
          <el-radio :label="false">是</el-radio>
          <el-radio :label="true">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="密码" prop="userPassword">
        <el-input
          type="password"
          v-model="ruleForm.userPassword"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确认修改</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="handleBack()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "user-edit",
  data() {
    var checkNickName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("昵称不能为空"));
      } else {
        const find = { nickName: value };
        if (value === this.ruleForm.nickName2) {
          this.$store.dispatch("users/search", find).then((res) => {
            if (res.length > 1) {
              return callback(new Error("昵称已被使用"));
            }
            return callback();
          });
        } else {
          this.$store.dispatch("users/search", find).then((res) => {
            if (res.length != 0) {
              return callback(new Error("昵称已被使用"));
            }
            return callback();
          });
        }
      }
    };
    var checkuserName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      } else {
        const find = { userName: value };
        if (value === this.ruleForm.userName) {
          this.$store.dispatch("users/search", find).then((res) => {
            if (res.length > 1) {
              return callback(new Error("账号已被使用"));
            }
            callback();
          });
        } else {
          this.$store.dispatch("users/search", find).then((res) => {
            if (res.length != 0) {
              return callback(new Error("账号已被使用"));
            }
            callback();
          });
        }
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.userPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        _id: "",
        nickName: "",
        userPassword: "",
        checkPass: "",
        userName: "",
        status: "",
      },
      rules: {
        userPassword: [{ validator: validatePass, trigger: "blur" }],
        nickName: [{ validator: checkNickName, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        userName: [{ validator: checkuserName, trigger: "blur" }],
        status: [
          { required: true, message: "请选择是否注销", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    const obj = this.$route.query;
    this.ruleForm._id = obj._id;
    this.ruleForm.userName = obj.userName;
    this.ruleForm.userName2 = obj.userName;
    this.ruleForm.nickName = obj.nickName;
    this.ruleForm.nickName2 = obj.nickName;
    this.ruleForm.status = obj.status;
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const {
            userName,
            userPassword,
            _id,
            status,
            nickName,
          } = this.ruleForm;
          this.$store
            .dispatch(`users/update`, {
              _id,
              userName,
              userPassword,
              status,
              nickName,
            })
            .then((res) => {
              console.log(res);
              if (res) {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.$router.push("/user");
              } else {
                this.$message({
                  message: "修改失败",
                  type: "error",
                });
              }
              this.ruleForm.userPassword = this.ruleForm.userName = this.ruleForm.checkPass = this.ruleForm.nickName =
                "";
              this.$router.push("/user/list");
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleBack() {
      this.$router.back();
    },
    test() {
      console.log(this.ruleForm);
      console.log(this.ruleForm.status);
    },
  },
};
</script>

<style lang="scss" scoped>
.user-edit {
  width: 50%;
  margin: 6% auto;
}
</style>